package com.samoilovich.belarustoday.handlers;

import android.util.Log;

import com.samoilovich.belarustoday.datemodel.CategoryModel;
import com.samoilovich.belarustoday.basefragments.NewsBaseFragment;
import com.samoilovich.belarustoday.tools.Utility;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.InputStream;
import java.util.ArrayList;

public class AfishaHandler {
    private final String TAG = getClass().getName();
    private final String AFISHA_CATEGORIES_URL = "http://belarustoday.by/belarus/api/afishacategory.php";

    private final String NAME = "afisha_category_name";
    private final String IMAGE_LINK = "afisha_category_image";
    private final String TYPE = "afisha_category_type";

    private static AfishaHandler sInstance;
    private ArrayList<CategoryModel> mCategories;

    private AfishaHandler() {
        mCategories = new ArrayList<CategoryModel>();
    }

    public static AfishaHandler getInstance() {
        if (sInstance == null) {
            sInstance = new AfishaHandler();
        }

        return sInstance;
    }

    public void getCategories(final NewsBaseFragment listener) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpResponse response;
                HttpClient client;
                HttpParams params;

                try {
                    if (listener != null) {
                        params = Utility.createHttpParams();
                        client = new DefaultHttpClient(params);
                        response = client.execute(new HttpGet(AFISHA_CATEGORIES_URL));
                        InputStream stream = response.getEntity().getContent();
                        String json = Utility.convertStreamToString(stream);
                        parseCategoriesJSON(json);
                        Log.d(TAG, "mCategories = " + (mCategories == null) + "\n listener = " + (listener == null));
                        listener.update(mCategories);
                        Log.d(TAG, json);
                    }
                } catch (Exception exp) {
                    exp.printStackTrace();
                }
            }
        }).start();
    }

    // -----------------------------------------------------------------------------------
    // -----------------------------------------------------------------------------------
    private void parseCategoriesJSON(String json) {
        JSONObject temp;
        CategoryModel category;

        try {
            mCategories = new ArrayList<CategoryModel>();
            JSONArray array = new JSONArray(json);
            for (int i = 0; i < array.length(); i++) {
                category = new CategoryModel();
                temp = array.getJSONObject(i);
                category.setName(temp.getString(NAME));
                category.setImageLink(temp.getString(IMAGE_LINK));
                category.setType(temp.getInt(TYPE));
                mCategories.add(category);
            }

        } catch (Exception exp) {
            exp.printStackTrace();
        }
    }
}
